//
//  EWEPlayer.m
//  Auscultation
//
//  Created by 来 on 2020/12/6.
//

#import "EWEPlayer.h"

@implementation EWEPlayer

- (instancetype)init
{
    self = [super init];
    if (self) {
        player = [[EWEDataPlayer alloc] init];
        iii = 0;
        isPause = false;
    }
    return self;
}

- (void)dealloc
{
    [self stop];
    if (player) {
        player = nil;
    }
}

- (void)play {
    if (!sendDataTimer) {
        NSLog(@"开始播放");
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        sendDataTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
        dispatch_source_set_timer(sendDataTimer, dispatch_walltime(NULL, 0), (1.0 / 40.0)*NSEC_PER_SEC,  0); //每秒执行
        dispatch_source_set_event_handler(sendDataTimer, ^{
            [self readNextPCMData];
        });
        dispatch_resume(sendDataTimer);
    }
}

- (void)readNextPCMData
{
    if ((iii * EVERY_READ_LENGTH + EVERY_READ_LENGTH + EVERY_READ_LENGTH) <= (self.fdatas.length))  {
        NSData *sub = [self.fdatas subdataWithRange: NSMakeRange(iii * EVERY_READ_LENGTH, EVERY_READ_LENGTH)];
        [player play: sub];
        [player start];
        iii++;
    } else {
        NSLog(@"数据不够用了, 已经暂停播放");
        NSLog(@"已接收总数据:%ld", (long)self.fdatas.length);
        NSLog(@"已播放数据:%ld", (long)(iii * EVERY_READ_LENGTH + EVERY_READ_LENGTH + EVERY_READ_LENGTH));
        if (self.pausePlayer) {
            self.pausePlayer();
            dispatch_suspend(sendDataTimer);
            [player pause];
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (!self->isPause) {
                if (self.startPlayer) {
                    self.startPlayer();
                    [self->player start];
                    dispatch_resume(self->sendDataTimer);
                }
                NSLog(@"恢复播放");
            }
        });
    }
}

- (void)pause {
    isPause = true;
}

- (void)resume {
    if (self.startPlayer) {
        self.startPlayer();
        [player start];
        dispatch_resume(sendDataTimer);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self->isPause = false;
        });
        NSLog(@"听诊引导恢复播放");
    }
}

- (void)stop {
    if (sendDataTimer) {
        dispatch_source_cancel(sendDataTimer);
    }
    if (player) {
        [player stop];
    }
}

- (void)clearData {
    if (self.datas) {
        [self.datas resetBytesInRange:NSMakeRange(0, [self.datas length])];
        [self.datas setLength:0];
    }
    if (self.fdatas) {
        [self.fdatas resetBytesInRange:NSMakeRange(0, [self.fdatas length])];
        [self.fdatas setLength:0];
    }
    iii = 0;
}

- (NSMutableData *)fdatas {
    if (!_fdatas) {
        _fdatas = [NSMutableData new];
    }
    return _fdatas;
}

- (NSMutableData *)datas {
    if (!_datas) {
        _datas = [NSMutableData new];
    }
    return _datas;
}

@end
